package PuzzlePieces;

import java.util.ArrayList;

public abstract class SudokuLine {

	protected int[] committedLine = new int[9];
	protected int[] tempLine  = new int[9];
	protected ArrayList<Integer> tempUsedNumbers = new ArrayList<Integer>(9);
	protected ArrayList<Integer> committedUsedNumbers = new ArrayList<Integer>(9);
	protected ArrayList<Integer> tempAvailNumbers = new ArrayList<Integer>(9);
	private int numTempZeros = 9;
	private int numCommitZeros = 9;
	private int ID;
	
	public void setNumber(int position, int value){
		tempLine[position] = value;
		tempUsedNumbers.add(new Integer(value));
		tempAvailNumbers.remove(new Integer(value));
	}
	
	public SudokuLine(int ID){
		this.ID = ID;
		setupAvailNumbers();
	}
	
	private void setupAvailNumbers() {

		for(int i = 0; i < 9; i++){
			tempAvailNumbers.add(new Integer(i+1));
		}
		
	}

	public void commit(){
		committedUsedNumbers.clear();
		for(int i = 0; i < tempLine.length; i++){
			committedLine[i] = tempLine[i];
			if(tempLine[i] != 0){
				committedUsedNumbers.add(new Integer(tempLine[i]));
			}
		}
		numCommitZeros = numTempZeros;
	}
	
	public void reset(){
		tempUsedNumbers.clear();
		setupAvailNumbers();
		for(int i = 0; i < tempLine.length; i++){
			tempLine[i] = committedLine[i];
			if(committedLine[i] != 0){
				tempUsedNumbers.add(new Integer(committedLine[i]));
				tempAvailNumbers.remove(new Integer(committedLine[i]));
			}
		}
		numTempZeros = numCommitZeros;
	}
	
	public ArrayList<Integer> getUsedNumbers(){
		return tempUsedNumbers;
	}
	
	public ArrayList<Integer> getAvailNumbers(){
		return tempAvailNumbers;
	}
	
	public boolean correct(){
		boolean result = true;
		ArrayList<Integer> myNumbers = new ArrayList<Integer>(9);
		for(int i = 0; i < tempLine.length; i++){
			if(myNumbers.contains(new Integer(tempLine[i]))){
				result = false;
			}
			myNumbers.add(new Integer(tempLine[i]));
		}
		return result;
	}
	
	public String toString(){
		StringBuilder myString = new StringBuilder();
		for(int i = 0; i < committedLine.length; i++){
			myString.append(committedLine[i]+" ");
		}
		myString.append("\n");
		return myString.toString();
	}
	
	public String tempString(){
		StringBuilder myString = new StringBuilder();
		for(int i = 0; i < tempLine.length; i++){
			myString.append(tempLine[i]+" ");
		}
		myString.append("\n");
		return myString.toString();
	}
	
	public int getID(){
		return ID;
	}
	
	public int getNumber(int position){
		return tempLine[position];
	}
	
	public int[] getNumbers(){
		return tempLine;
	}
}
